package cn.edu.nuc.onlinestore.vo;

import java.io.File;
import java.io.Serializable;

import cn.edu.nuc.onlinestore.model.User;
import cn.edu.nuc.onlinestore.stream.ObjectStream;

public class UserDo implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 2164077805853828087L;
	private String username;
	private String password;
	private String msg;
	public UserDo(String userbane,String password){
		File file=new File("D:/store/user");
		if(!file.exists()){
			file.mkdirs();
		}
		this.username=userbane;
		this.password=password;
	}
	/**
	 * 注册用户，并将其写入文件
	 * @return
	 */
	public boolean userAdd(){
		File file=new File("D:/store/user/"+username+".txt");
		if(file.exists()){
			msg="用户名已存在，请直接登录";
			return false;
		}else{
			ObjectStream.write("d:/store/user/"+username+".txt",new User(username,password));
			msg="创建成功,您的用户名为:"+username+"密码为:"+password;
		}
		return false;
	}
	public boolean userUpdatepassword(String newpass){
		User user=ObjectStream.read(User.class,"d:/store/user/"+username+".txt");
		user.setPassword(newpass);
		ObjectStream.write("user/"+username+".txt",user);
		msg="更改密码成功";
		return true;
	}
	/**
	 * 从文件中读取用户名密码,判断登录
	 * @return
	 */
	public boolean userCheck(){
		File file=new File("D:/store/user/"+username+".txt");
		if(file.exists()){
			//执行文件读取操作
			User user=ObjectStream.read(User.class,"D:/store/user/"+username+".txt");
			if(password.equals(user.getPassword())){
				msg="恭喜您,"+username+"登陆成功";
				return true;
			}else{
				msg="密码不正确";
			}
		}else{
			//返回是否注册提示页面
				msg="该用户名不存在";
				return false;
		}
		return false;
	}
	public String getMsg() {
		return msg;
	}
}
